//
//  LTChooseClassView.m
//  LearnTeacher
//
//  Created by zyl on 2024/4/12.
//

#import "LTChooseClassView.h"

@interface LTChooseClassView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@end
@implementation LTChooseClassView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    self.backgroundColor = rgba(0, 0, 0, 0.00);
    self.hidden = YES;
    [self addSubview:self.tableView];
    self.clipsToBounds = YES;
   
    return self;
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint p = [touch locationInView:self];
    if (p.y > self.tableView.height) {
       [self closeView];
    }
    
}
- (void)openView
{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.y = 0;
        self.backgroundColor = rgba(0, 0, 0, 0.60);
    }];
    
}
- (void)closeView
{
 [UIView animateWithDuration:0.2 animations:^{
     self.tableView.y = - self.tableView.height;
    self.backgroundColor = rgba(0, 0, 0, 0.00);
 } completion:^(BOOL finished) {
     self.hidden = YES;
     if (self.block) {
         self.block(self.selectedClass);
     }
 }];
}
- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.tableView reloadData];
    self.tableView.height = (dataArray.count + 1)*51;
    self.tableView.y = - self.tableView.height;
    [self.tableView setCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadius:20];
}

#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    
    //
    [cell removeAllSubviews];
    NSString *name = @"全部";
    if (indexPath.row > 0) {
        NSDictionary *dic = self.dataArray[indexPath.row - 1];
        name = [NSString stringWithFormat:@"%@",dic[@"grade_name"]];
    }
    
    
    UILabel *detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, LTScreenWidth - 60,51)];
    detailTextLabel.textAlignment = NSTextAlignmentLeft;
    detailTextLabel.textColor = LTTextBlack;
    detailTextLabel.font = RFontWithSize(14);
    detailTextLabel.text = name;
    [cell addSubview:detailTextLabel];
    if (indexPath.row == self.selectedClass) {
        UIImageView *img =  [[UIImageView alloc] initWithImage:IMAGENAMED(@"ic_homework_class_selected")];
        [img setFrame:CGRectMake(LTScreenWidth - 20 - 16, (51 - 12)/2.0, 16, 12)];
        [cell addSubview:img];
    }
   
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count + 1;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 51;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ( self.selectedClass != indexPath.row) {
        self.selectedClass = indexPath.row;
        [self.tableView reloadData];
       
        
    }
    [self closeView];
    
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    
    if (!_tableView){
        
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, LTScreenWidth,30) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:LTWhiteColor];
        [_tableView setSeparatorColor:rgba(231, 231, 231, 1)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];

    }
    
    return _tableView;
}
@end
